package org.karelun.gef.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.karelun.gef.dom.ReadClassDiagram;
import org.karelun.gef.dom.ReadStateDiagram;
import org.karelun.gef.dom.ReadUserCaseDiagram;
import org.karelun.gef.editors.ClassDiagramEditor;
import org.karelun.gef.editors.DiagramEditorInput;
import org.karelun.gef.editors.StateDiagramEditor;
import org.karelun.gef.editors.UserCaseDiagramEditor;
import org.karelun.gef.helper.ConstantMessages;
import org.karelun.gef.helper.IDConstant;
import org.karelun.gef.helper.ImageResource;
import org.karelun.gef.utils.DOMUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class OpenFileAction extends Action {
	
	private IWorkbenchWindow window;
	private Element rootEle = null;
	
	public OpenFileAction(IWorkbenchWindow window) {
		this.window = window;
		setId(IDConstant.OpenFileAction_Id);
		setText(ConstantMessages.openFile);
		setImageDescriptor(ImageResource.getImageDesc(ImageResource.OPEN));
		setActionDefinitionId(IDConstant.OpenFileAction_CMD_ID);
	}

	public void run() {
		String path = openFileDialog();
		if (path == null)
			return;
		File file = new File(path);
		try {
			FileInputStream in = new FileInputStream(file);
			Document document = DOMUtils.getDocumentBuilder().parse(in);
			rootEle = DOMUtils.getFirstChildElement(document);
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		if (rootEle == null)
			return;
		String name = rootEle.getNodeName();
		if (name == null)
			return;
		DiagramEditorInput input = new DiagramEditorInput(new Path(path));
		IWorkbenchPage page = window.getActivePage();
		try {
			if ("classDiagram".equals(name)) { //$NON-NLS-1$
				IEditorPart editorPart = page.openEditor(input,
						IDConstant.ClassDiagramEditor_Id, true);
				ClassDiagramEditor editor = (ClassDiagramEditor) editorPart;
				loadClassDiagram(editor);
			}else if("userCaseDiagram".equals(name)){ //$NON-NLS-1$
				IEditorPart editorPart = page.openEditor(input,
						IDConstant.UserCaseDiagramEditor_Id, true);
				UserCaseDiagramEditor editor=(UserCaseDiagramEditor)editorPart;
				loadUserCaseDiagram(editor);
			}else if("stateDiagram".equals(name)){ //$NON-NLS-1$
				IEditorPart editorPart = page.openEditor(input,
						IDConstant.StateDiagramEditor_Id, true);
				StateDiagramEditor editor=(StateDiagramEditor)editorPart;
				loadStateDiagram(editor);
			}
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}

	private void loadClassDiagram(ClassDiagramEditor editor) {
		ReadClassDiagram classDiagram = new ReadClassDiagram(editor);
		classDiagram.readXML(rootEle);
	}

	private void loadUserCaseDiagram(UserCaseDiagramEditor editor) {
		ReadUserCaseDiagram userCaseDiagram = new ReadUserCaseDiagram(editor);
		userCaseDiagram.readXML(rootEle);
	}

	private void loadStateDiagram(StateDiagramEditor editor) {
		ReadStateDiagram stateDiagram = new ReadStateDiagram(editor);
		stateDiagram.readXML(rootEle);
	}

	private String openFileDialog() {
		FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN);
		dialog.setText(ConstantMessages.OpenFileDialog_Title);
		 dialog.setFilterExtensions(new String[]{"*.xml"}); //$NON-NLS-1$
		return dialog.open();
	}
}
